package com.luckmgr.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.luckmgr.entity.db.Activity;
import com.luckmgr.service.ActivityService;

@Component
public class ActivityCore extends Thread {
	private Logger logger = Logger.getLogger(ActivityCore.class);
	Long intervalTime = 15L;

	@Autowired
	ActivityService activityServiceImpl;

	@Override
	public void run() {
		while (true) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				List<Activity> activitylist = activityServiceImpl
						.selectList(map);
				if (null != activitylist) {
					for (int i = 0; i < activitylist.size(); i++) {
						Activity activity = activitylist.get(i);
						if(activity.getStatus()==0 || 1==activity.getStatus()){
							long systemTime = System.currentTimeMillis();
							if(null != activity.getStartTime() && null != activity.getEndTime()){
								if(activity.getStartTime().getTime()<systemTime && activity.getEndTime().getTime()>systemTime){
									if(activity.getStatus()==0){
										activityServiceImpl.updateStatus(activity.getId(), 1);
									}
								}else if(activity.getEndTime().getTime()<systemTime){
									if(activity.getStatus()==1){
										activityServiceImpl.updateStatus(activity.getId(), 2);
									}
								}
							}
						}
					}
				}
				Thread.sleep(intervalTime * 1000);
			} catch (Exception e) {
				logger.info(e.getMessage(), e);
			}
		}
	}

}
